Skip to content

Conversation

@akshayutture-augment
Copy link
Contributor

* wip

* Add working actions for GMA rules based on Prom-only API

* Remove Ruler-loader related code for Grafana rules

Co-authored-by: Sonia Augilar <[email protected]>

* Remove outdated tests

* add some comments

* remove commented code

* remove showLocation property

* Add missing mocks in tests

* Add showLocation to GrafanaRuleListItem, improve useAbilities, address PR feedback

* Enhance GrafanaGroupLoader tests: Add permission checks and More button functionality

- Introduced user permission grants for alerting actions in tests.
- Added tests for rendering the More button with action menu options.
- Verified that each rule has its own action buttons and handles permissions correctly.
- Ensured the edit button is not rendered when user lacks edit permissions.
- Confirmed the correct menu actions are displayed when the More button is clicked.

* Update translations

---------

Co-authored-by: Sonia Aguilar <[email protected]>
Co-authored-by: Sonia Augilar <[email protected]>
@akshayutture-augment
Copy link
Contributor Author

@claude review this PR

1 similar comment
@akshayutture-augment
Copy link
Contributor Author

@claude review this PR

@claude
Copy link

claude bot commented Nov 20, 2025

Claude finished @akshayutture-augment's task —— View job


PR Review: Notification Rule Processing Engine

Summary

This PR refactors the alerting rule list view to work exclusively with the Prometheus API, removing dependencies on the Ruler API for Grafana-managed rules. This is a significant architectural change that simplifies the code and improves performance by eliminating redundant API calls.


🎯 Key Changes

  1. New GrafanaPromRuleDTO support in abilities hooks - Added useAllGrafanaPromRuleAbilities and related hooks to work directly with Prometheus DTOs
  2. Simplified GrafanaGroupLoader - Removed Ruler API dependency and rule-matching logic (~94 lines deleted)
  3. New GrafanaRuleListItem component - Extracted from GrafanaRuleLoader.tsx to handle individual rule rendering
  4. Enhanced AlertRuleMenu and RuleActionsButtons - Now support both Ruler and Prometheus rule DTOs for backward compatibility
  5. Comprehensive test updates - Added permission checks and action button verification

✅ Strengths

  1. Performance improvement: Eliminates redundant Ruler API calls, reducing network overhead
  2. Code simplification: Removes complex rule-matching logic (94 lines removed from GrafanaGroupLoader)
  3. Good backward compatibility: Dual support for both RulerRuleDTO and GrafanaPromRuleDTO prevents breaking changes
  4. Solid test coverage: Tests verify permissions, button rendering, and menu actions
  5. Clear separation of concerns: New GrafanaRuleListItem component is well-structured

⚠️ Issues & Concerns

Critical Issues

1. Incomplete TODO comment (useAbilities.ts:242-243)

// TODO: Add support for federated rules
// const isFederated = isFederatedRuleGroup();
const isFederated = false;

Issue: Hard-coding isFederated = false could cause incorrect permission checks for federated rules if they're added later.
Recommendation: Either implement federated rule support or add a clearer explanation of why it's not needed for GrafanaPromRuleDTO.

2. Potential null safety issue (GrafanaGroupLoader.tsx:68)

{promResponse.data.groups.at(0)?.rules.map((promRule) => {

Issue: While .at(0)?.rules is safe, if groups[0] is undefined, .map() will throw an error on undefined.
Fix needed:

{promResponse.data.groups.at(0)?.rules?.map((promRule) => {

or better yet:

{(promResponse.data.groups.at(0)?.rules ?? []).map((promRule) => {

Medium Priority Issues

3. Duplicate comment in useAbilities.ts:289

const { isEditable, isRemovable, loading } = useIsGrafanaPromRuleEditable(rule); // duplicate

Issue: The comment "// duplicate" appears to be leftover from development.
Fix: Remove the comment.

4. Missing validation in AlertRuleMenu.tsx:73

const [grafanaPauseAbility, ...] = useGrafanaPromRuleAbilities(
  prometheusRuleType.grafana.rule(promRule) ? promRule : skipToken, [...]
);

Concern: Similar conditional checks are repeated 5 times (lines 73, 81, 85, 89, 93). Consider extracting to a variable:

const grafanaRule = prometheusRuleType.grafana.rule(promRule) ? promRule : skipToken;
const [grafanaPauseAbility, grafanaDeleteAbility, ...] = useGrafanaPromRuleAbilities(grafanaRule, [...]);

5. Inconsistent ability merging logic (AlertRuleMenu.tsx:83-96)

const canPause = (pauseSupported && pauseAllowed) || (grafanaPauseSupported && grafanaPauseAllowed);

Issue: This OR logic means if either Ruler or Prometheus abilities allow an action, it's permitted. This could be a security concern if one API is more permissive than the other.
Question: Is this intentional? Should we use AND logic instead to be more restrictive?

Minor Issues

6. Hard-coded default value (GrafanaGroupLoader.tsx:32)

expectedRulesCount = 3, // 3 is a random number. Usually we get the number of rules from Prometheus response

Issue: Magic number with unclear justification.
Recommendation: Use a named constant like DEFAULT_SKELETON_COUNT = 3 for better maintainability.

7. Type narrowing could be improved (RuleActionsButtons.V2.tsx:139-144)

function getEditableIdentifier(...): EditableRuleIdentifier | undefined {
  if (rule) {
    return ruleId.fromRulerRuleAndGroupIdentifierV2(groupIdentifier, rule);
  }
  if (prometheusRuleType.grafana.rule(promRule)) {
    return { ruleSourceName: 'grafana', uid: promRule.uid } satisfies GrafanaRuleIdentifier;
  }

Issue: TypeScript can't guarantee promRule.uid exists after the type guard.
Safer approach:

if (prometheusRuleType.grafana.rule(promRule) && promRule) {

8. Error boundary consideration
The GrafanaGroupLoader component doesn't have error handling for the case where promResponse exists but groups[0] is missing. Consider adding a fallback or better error message.

9. Translation keys removed (grafana.json)
Two translation keys were removed without visible replacements in the code. Ensure these aren't still referenced elsewhere in the codebase.


🧪 Testing Observations

Positive:

  • Comprehensive test coverage for permission checks
  • Tests verify menu action availability
  • Tests confirm correct button rendering based on permissions

Suggestions:

  • Add tests for the null/undefined edge cases mentioned above
  • Consider adding integration tests for the Prometheus-only flow
  • Test the transition behavior when Ruler API is unavailable

📋 Code Quality

Good practices:

  • Proper use of useMemo for expensive computations
  • Clear deprecation markers (@deprecated JSDoc)
  • Meaningful variable names
  • Good component composition

Areas for improvement:

  • Some functions are quite long (e.g., useAllGrafanaPromRuleAbilities has 50+ lines)
  • Consider extracting the permission merging logic into a utility function
  • The skipToken pattern is clever but could use better documentation

🔍 Security Considerations

  1. Permission checks: The dual-path permission logic (Ruler + Prometheus) needs careful review to ensure it doesn't inadvertently grant unauthorized access
  2. Folder permissions: The new useIsGrafanaPromRuleEditable correctly checks folder-level permissions
  3. Admin-only actions: DeletePermanently correctly requires isAdmin() check

📝 Documentation

Missing:

  • No inline documentation explaining why the Ruler API was removed
  • The architectural decision to use Prometheus-only should be documented
  • The transition strategy for existing consumers should be explained

Suggested additions:

  • Add a comment in GrafanaGroupLoader explaining the Prometheus-only approach
  • Document the skipToken pattern in the abilities hooks
  • Add JSDoc to useAllGrafanaPromRuleAbilities explaining when to use it vs useAllRulerRuleAbilities

🎬 Recommendations

Before merging:

  1. ✅ Fix the critical null safety issue in GrafanaGroupLoader.tsx:68
  2. ✅ Remove the "// duplicate" comment
  3. ✅ Address or document the federated rules TODO
  4. ✅ Review the permission merging logic for security implications
  5. ✅ Add tests for edge cases

Nice to have:

  • Extract repeated conditional checks in AlertRuleMenu
  • Use named constant for skeleton count
  • Add architectural decision documentation
  • Consider performance impact of multiple hook calls in AlertRuleMenu

📊 Overall Assessment

Impact: High - This is a significant architectural change affecting core alerting functionality

Risk: Medium - The backward compatibility approach mitigates risk, but the permission logic needs careful validation

Code Quality: Good - Well-structured with appropriate abstractions, but some edge cases need attention

Test Coverage: Good - Comprehensive tests for the happy path, some edge cases could be added

Recommendation: ✅ Approve with changes - Fix the critical null safety issue and review the permission merging logic before merging. The overall direction is solid and represents a meaningful improvement to the codebase.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants